package com.zsummer.zero.xml;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.thoughtworks.xstream.XStream;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class Start {
	
	public static void main(String[] args) throws IOException {
		String path = "C:\\Users\\zsummer\\Desktop\\a.xml";
		String dest = path.replace(".xml", ".xls");
		File from = new File(path);
		File to = new File(dest);
		XStream xStream = new XStream();
		
		xStream.processAnnotations(new Class[] {Person.class, Data.class});
		
//		Data data = new Data();
//		data.setHome("zhang");
//		Person person = new Person();
//		person.setName("a");
//		person.setAge(1);
//		List<Person> persons = new ArrayList<Person>();
//		persons.add(person);
//		data.setList(persons);
//		xStream.toXML(data, new FileOutputStream(new File(path)));
		
		WritableWorkbook workBook = Workbook.createWorkbook(to);
		WritableSheet sheet = workBook.createSheet(from.getName(), 0);
		
		Data data = (Data) xStream.fromXML(new File(path));
		List<Person> list = data.getList();
		int i = 0;
		for (Person person : list) {
			i++;
			try {
				sheet.addCell(new Label(0, i, data.getHome()));
				sheet.addCell(new Label(1, i, person.getName()));
				sheet.addCell(new Label(2, i, String.valueOf(person.getAge())));
			} catch (RowsExceededException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (WriteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		workBook.write();
		try {
			workBook.close();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("finish");
	}

}
